<?php

/**
 * Sunny软件公司欲为某高校开发一套奖励审批系统，该系统可以
 * 实现教师奖励和学生奖励的审批(Award Check)，如果教师发表论文数超过10篇或者
 * 学生论文超过2篇可以评选科研奖，如果教师教学反馈分大于等于90分或者学生平均
 * 成绩大于等于90分可以评选成绩优秀奖。试使用访问者模式设计该系统，以判断候选
 * 人集合中的教师或学生是否符合某种获奖要求。
 */

interface Employee
{
	//接受一个抽象访问者访问
	public function accept(Award $award);
}

class Teacher implements Employee
{
	private $_name;
	private $_pager;
	private $_feedback;
	
	public function __construct($name, $pager, $feedback)
	{
		$this->_name = $name;
		$this->_pager = $pager;
		$this->_feedback = $feedback;
	}
	
	public function getName()
	{
		return $this->_name;
	}
	
	public function getPager()
	{
		return $this->_pager;
	}
	
	public function getFeedback()
	{
		return $this->_feedback;
	}
	
	public function accept(Award $award)
	{
		$award->visitTeacher($this);
	}
}

class Student implements Employee
{
	private $_name;
	private $_pager;
	private $_average;
	
	public function __construct($name, $pager, $average)
	{
		$this->_name = $name;
		$this->_pager = $pager;
		$this->_average = $average;
	}
	
	public function getName()
	{
		return $this->_name;
	}
	
	public function getPager()
	{
		return $this->_pager;
	}
	
	public function getAverage()
	{
		return $this->_average;
	}
	
	public function accept(Award $award)
	{
		$award->visitStudents($this);
	}
}

/**
 * 抽象访问类
 */
abstract class Award
{
	//访问不同类型的被访问者
	public abstract function visitTeacher(Teacher $teacher);
	public abstract function visitStudents(Student $student);
}


/**
 * 科研奖
 */
class ScientificAward extends Award
{
	public function visitTeacher(Teacher $teacher)
	{
		$pagerNo = $teacher->getPager();
		$temp = '符合';
		if ($pagerNo <= 10) {
			$temp = '不符合';
		}
		echo "教师：{$teacher->getName()} 论文：{$pagerNo}篇 {$temp}科研奖。<br/>";
	}
	
	public function visitStudents(Student $student)
	{
		$pagerNo = $student->getPager();
		$temp = '符合';
		if ($pagerNo <= 2) {
			$temp = '不符合';
		}
		echo "学生：{$student->getName()} 论文：{$pagerNo}篇 {$temp}科研奖。<br/>";
	}
}

/**
 * 成绩优秀奖
 */
class ExcellentAward extends Award
{
	public function visitTeacher(Teacher $teacher)
	{
		$feedbackScore = $teacher->getFeedback();
		$temp = '符合';
		if ($feedbackScore < 90) {
			$temp = '不符合';
		}
		echo "教师：{$teacher->getName()} 教学反馈封：{$feedbackScore}  {$temp}成绩优秀奖。<br/>";
	}
	
	public function visitStudents(Student $student)
	{
		$averageScore = $student->getAverage();
		$temp = '符合';
		if ($averageScore < 90) {
			$temp = '不符合';
		}
		echo "学生：{$student->getName()} 平均成绩：{$averageScore}  {$temp}成绩优秀奖。<br/>";
	}
}

class EmployeeList
{
	private $_employees = array();
	
	public function attach(Employee $employee)
	{
		$this->_employees[] = $employee;
	}
	
	public function detach(Employee $employee)
	{
		$index = array_search($this->_employees, $employee, true);
		if ($index !== false) {
			unset($this->_employees[$index]);
		}
	}
	
	public function accept(Award $award)
	{
		foreach ($this->_employees as $employee) {
			$employee->accept($award);
		}
	}
}

class Client
{
	public static function main()
	{
		$list = new EmployeeList();
		
		$teacher1 = new Teacher('张三', 9, 80);
		$teacher2 = new Teacher('李四', 11, 95);
		$teacher3 = new Teacher('王五', 11, 85);
		$teacher3 = new Teacher('赵六', 5, 98);
		
		$student1 = new Student('陈七', 3, 95);
		$student2 = new Student('嫣八', 1, 95);
		
		$list->attach($teacher1);
		$list->attach($teacher2);
		$list->attach($teacher3);
		$list->attach($student1);
		$list->attach($student2);
		
		$award = new ScientificAward();
		echo '科研奖：教师论文大于10篇，学生大于2篇<br/>';
		$list->accept($award);
		
		$award = new ExcellentAward();
		echo '成绩优秀奖：教师教学反馈分大于等于90，学生平均成绩大于等于90<br/>';
		$list->accept($award);
	}
}

Client::main();